﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Add_CustomClearance : Form
    {
        public frm_Add_CustomClearance()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            INISettings mySettings = new INISettings();
            SqlConnection myConnection = new SqlConnection(mySettings.SQLConnectionString);
            bool duplicate = false;
            if (clearanceNo.Text == "")
            {
                MessageBox.Show("Please enter Clearance No.");
            }
            else
            {
                //Check Duplicate
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM \"241_CustomClearance\" WHERE ID='" + clearanceNo.Text + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        duplicate = true;
                    }
                    dataReader.Close();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    duplicate = true;
                }

                if (duplicate)
                {
                    MessageBox.Show("The Clearance No. " + clearanceNo.Text + " existed");
                }
                else
                {
                    try
                    {
                        DateTime register;
                        DateTime.TryParse(registeredDate.DateTime.Date.ToString(), out register);
                        DateTime finish;
                        DateTime.TryParse(finishedDate.DateTime.Date.ToString(), out finish);
                        myConnection.Open();
                        string sql = "INSERT INTO \"241_CustomClearance\" "
                                + "(ID,RegisterDate,FinishedDate,Note) "
                                + "VALUES ('" + clearanceNo.Text + "',@register,@finished,'" + noteMemo.Text + "')";                                
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.Parameters.Add("@register", SqlDbType.DateTime).Value = register.Date;
                        cmd_sql.Parameters.Add("@finished", SqlDbType.DateTime).Value = finish.Date;
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("Update Success!");
                        //this.Dispose();
                        clearForm();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Update failed!");
                    }
                }
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
